package log350.tp3;

import classes.Billet;
import classes.UserHandler;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Paint;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class AcheterBilletActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_acheter_billet);
        
    	creerPath();
    	
        String[] arrayType = new String[3];
        arrayType[0] = "BYOC";
        arrayType[1] = "Console";
        arrayType[2] = "Accompagnateur";
        
        Spinner s = (Spinner) findViewById(R.id.spnBillet);
        @SuppressWarnings({ "rawtypes", "unchecked" })
		ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, arrayType);
        s.setAdapter(adapter);
    }
    
    public void acheterBillet(View view){
     	EditText txtSiege = ((EditText) findViewById(R.id.txtSiege));
    	String siege = txtSiege.getText().toString();
    	
    	if (siege.trim().equals("")){
    		AlertDialog.Builder alert = new AlertDialog.Builder(this);
    		alert.setTitle("ERREUR");
    		alert.setMessage("Veuillez remplir tous les champs.");
    		alert.setPositiveButton("OK", new android.content.DialogInterface.OnClickListener() {                
    	        public void onClick(DialogInterface dialog, int which) {
    	        }
    		 });
    		alert.show();
    	}
    	else{
    		Spinner s = (Spinner) findViewById(R.id.spnBillet);
    		UserHandler.getInstance().getUserLoggedIn().setBillet(new Billet(s.getSelectedItem().toString(), siege));
    		setResult(RESULT_OK, new Intent());
            finish();
    	}
    	
    }

    public void back(View view){
    	setResult(RESULT_CANCELED, new Intent());
        finish();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_acheter_billet, menu);
        return true;
    }
    
    public void creerPath(){
    	TableLayout tableLayoutLink = (TableLayout)findViewById(R.id.tblLinkBillet);
        tableLayoutLink.removeAllViews();
        
        TableRow tableRowLink = new TableRow(getApplicationContext());            
        
        final TextView lanETS = new TextView(getApplicationContext());
        lanETS.setText("Lan ETS");  
        lanETS.setTextColor(0xFFff8000);
        lanETS.setTextSize(12);
        lanETS.setPaintFlags( lanETS.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
       
        lanETS.setPadding(15, 10, 0, 20);
        lanETS.setClickable(true);
        lanETS.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				setResult(RESULT_FIRST_USER, new Intent());
		        finish();
			}

          });
        tableRowLink.addView(lanETS);
        
        final TextView firstComma = new TextView(getApplicationContext());
        firstComma.setText(">");
        firstComma.setTextSize(12);
        firstComma.setPadding(10, 10, 0, 20);
        tableRowLink.addView(firstComma);
        
        final TextView billetLink = new TextView(getApplicationContext());
        billetLink.setText("Billets");  
        billetLink.setTextColor(0xFFff8000);
        billetLink.setTextSize(12);
        billetLink.setPaintFlags( lanETS.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
       
        billetLink.setPadding(15, 10, 0, 20);
        billetLink.setClickable(true);
        billetLink.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				setResult(RESULT_CANCELED, new Intent());
		        finish();
			}

          });
        tableRowLink.addView(billetLink);

        
        final TextView SecondComma = new TextView(getApplicationContext());
        SecondComma.setText(">");
        SecondComma.setTextSize(12);
        SecondComma.setPadding(10, 10, 0, 20);
        tableRowLink.addView(SecondComma);
        
        final TextView listeParticipant = new TextView(getApplicationContext());
        listeParticipant.setText("Acheter un Billet");   
        listeParticipant.setTextSize(12);        
        listeParticipant.setPadding(10, 10, 0, 20);
        tableRowLink.addView(listeParticipant);
        
        tableLayoutLink.addView(tableRowLink);
    }

}
